package com.ymttest.utils.ThreadPool;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.MessageCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class ThreadPool {
	private Map<String, FutureTask<String>> tasks = new HashMap<String, FutureTask<String>>();
	// 构造一个线程
	private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>());

	/**
	 * @param task
	 * @return
	 */
	public String addTask(Callable task) {
		FutureTask<String> futureTask = new FutureTask<String>(task);
		executor.execute(futureTask);
		String key = TestDataManager.getRandomUUID().toString();
		;
		tasks.put(key, futureTask);
		return key;
	}

	/**
	 * @param key
	 * @return
	 */
	public boolean taskIsDone(String key) {
		FutureTask<String> futureTask = tasks.get(key);
		if (futureTask != null) {
			return futureTask.isDone();
		}
		return false;
	}

	/**
	 * @param key
	 * @return
	 */
	public boolean taskIsCancelled(String key) {
		FutureTask<String> futureTask = tasks.get(key);
		if (futureTask != null) {
			return futureTask.isCancelled();
		}
		return false;
	}

	/**
	 * @param key
	 * @return
	 */
	public String getTaskResult(String key) {
		FutureTask<String> futureTask = tasks.get(key);
		if (futureTask.isDone()) {
			try {
				String result = futureTask.get();
				tasks.remove(key);
				return result;
			} catch (InterruptedException e) {
				e.printStackTrace();
				return null;
			} catch (ExecutionException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * @param task
	 * @return
	 */
	public String addTaskAndWaitResult(Callable task) {
		FutureTask<String> futureTask = new FutureTask<String>(task);
		executor.execute(futureTask);
		String key = TestDataManager.getRandomUUID().toString();
		tasks.put(key, futureTask);
		try {
			return futureTask.get();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return null;
		} catch (ExecutionException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
     * 
     */
	public void removeAllTask() {
		for (String key : tasks.keySet()) {
			executor.remove(tasks.get(key));
			tasks.remove(key);
		}
	}

	/**
	 * @param key
	 */
	public void removeQueryTask(String key) {
		executor.remove(tasks.get(key));
	}

	/**
	 * @param key
	 */
	public void removeTask(String key) {
		tasks.remove(key);
	}

	/**
     * 
     */
	public void clearTaskList() {
		tasks.clear();
	}

	public synchronized void stop() {
		try {
			executor.shutdownNow();
			executor.awaitTermination(1L, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			executor = null;
			tasks.clear();
			tasks = null;
		}
	}

	/**
	 * @param key
	 */
	public void cancelTask(String key) {

		FutureTask<String> futureTask = tasks.get(key);
		if (futureTask != null) {
			if (!futureTask.isDone()) {
				futureTask.cancel(true);
			}
		}
	}

	public void purgeCancelTask() {
		executor.purge();
		executor.getQueue();
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ThreadPool exec = new ThreadPool();
		for (int i = 0; i < 1000; i++) {
			Task task=new Task("http://counter.iapi.ymatou.com/version", "GET", "");
			exec.addTask(task);
			if(i%100==0){
				Logger.debug(i);
			}
			
		}
		try {
			Thread.sleep(1L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}
